#include <iostream> 
#include <algorithm>
using namespace std;

bool cmp(pair<int,int> &p1,pair<int,int> &p2){
	return p1.first < p2.first;
}
int main(){
	int n;
	cin >> n;
	pair<int,int> a[n+1];
	int v1,v2;
	for(int i=1; i<=n; i++){
		cin >> v1 >> v2;
		a[i] = make_pair(v1,v2);
	}
	sort(a+1,a+n+1,cmp);
	int max[n+1],sum[n+1];
	max[0] = -1;
	sum[0] = 0;
	int cnt = 0;
	int MAX = -1;
	for(int i=1; i<=n; i++){
		int k = i;
		while( max[--k]>a[i].second );
		if( sum[k]+1>sum[i-1] ){
			sum[i] = sum[k]+1;
			max[i] = a[i].second;
		}else if( sum[k]+1 <sum[i-1] ){
			sum[i] = sum[i-1];
			max[i] = max[i-1];
			if( a[i].second >= MAX ){
				cnt++;	
				MAX = a[i].second;
			} else{
				cnt = 0;
				MAX = a[i].second;
			}
			if( cnt>=sum[i] && MAX<max[i]){
				sum[i] = cnt;
				max[i] = MAX;
			}
		}else{
			sum[i] = sum[i-1];
			if( a[i].second<max[i-1] ){
				max[i] = a[i].second;
			}else{
				max[i] = max[i-1];
				if( a[i].second >= MAX ){
					cnt++;	
					MAX = a[i].second;
				} else{
					cnt = 0;
					MAX = a[i].second;
				}
				if( cnt>=sum[i] && MAX<max[i]){
					sum[i] = cnt;
					max[i] = MAX;
				}
			}
		}
	}
	cout << sum[n];
	return 0;
}
